<?php
/*=============================================
Drop-In Photo Gallery
Version 1.0

by Sam Perkins-Harbin
http://www.forge22.com

Last Revision: 17 August, 2006

You may modify this code as you wish, but please
send me any modifications you do, and keep my URL
on the footer!


This program uses the image resize script to generate
thumbnails on the fly for a directory. It parses
directory structure, and outputs navigation.
Thumbnails are generated for each image in a directory.
You can specify how many thumbs are displayed per page,
thumbnail size, and if folder navigation should be on.

CSS colors and layout are standard and editable as well.
CSS generates paragraph size based on specified size of
thumbnail. Each paragraph contains the thumbnail, image
dimensions and image filesize.

=============================================*/

$limit = 30; // number of thumbs to show per page.
$w = 180; // max width of thumbnails.
$h = 150; // max height of thumbnails.
$show_folders = 1; // Set to 1 to show folder list, 0 to turn off
$ititle = 1; // Set to 1 to display image name (title)
$idate = 1; // Set to 1 to display file date (modified)
$isize = 1; // Set to 1 to display image filesize
$idim = 1; // Set to 1 to display image dimensions

//============================================
// No further options below here.

if ($pg = $_GET['pg']) {
    $thumb = (($pg-1)*$limit);
    $i = $thumb;
}
//if($thumb = $_GET['thumb'])
//  $i = $thumb;
else
    $i = 0;

// get the directory folder name
$dirlist = dirname($_SERVER['SCRIPT_NAME']);

// split it up
$subdir = explode("/", $dirlist);

// reverse it so directory above images is listed
$subdir = array_reverse($subdir);

// page title name is root folder name
//$title = str_replace("_", " ", $subdir['0']);
$replace = array("_", "/");
$with = array(" ", " - ");
$title = str_replace($replace, $with, $_GET[d]);

//$my_uri = "http://" . $_SERVER['HTTP_HOST'] . $HTTP_SERVER_VARS["SCRIPT_NAME"];

if (ereg("\.\.", $_GET['d']))
    header("Location: ?");

if ($_GET['t'])
    $limit = $t;

// additional paramaters for <p> based on whether name or dimensions is displayed
$pwidth = $w+12;
$pheight = $h+12;
if ($ititle)
    $pheight +=10;

// increase height of thumbnail <p> element
if ($isize || $idim) {
    // if displaying dimensions or size of image, increase height of thumbnail <p> element.
    $pheight +=10;
}


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Photo Gallery - <?=$title?></title>
<style type="text/css">

body {
    background-color: #e6e6e6;
    text-align: center;
    margin: 0;
    padding: 1em;
    font-family: verdana;
    font-size: 12px;
    color: #000;
}

#container {
    width: 100%;
    margin: 0;
    text-align: center;
}

h1 {
    font-size: 16px;
    margin: 1em;
}

#dirnav {
    width: 100%;
    overflow: auto;
    position: relative;
    margin: auto auto 5px auto;
    clear: both;
    text-align: center;
    padding: 0;
    background-color: #fff;
    border: 1px solid #aaa;
}

#dirnav ul {
    width: 100%;
}

#dirnav ul, #dirnav li {
    margin: auto;
    padding: 0;
    list-style-type: none;
}

#dirnav li {
    width: 20%;
    border-bottom: 1px solid #ddd;
    border-right: 1px solid #ddd;
    margin-right: -1px;
    float: left;
    text-align: center;
}

#dirnav a {
    display: block;
    padding: 2px 5px 2px 5px;
}

#dirnav a:hover {
    background-color: #ddd;
    color: #000;
}

#thumbnails {

}

p.thumb {
    width: <? echo $pwidth; ?>px;
    height: <? echo $pheight; ?>px;
    margin: 1em;
    padding: 3px;
    float: left;
    font-size: 10px;
    text-align: center;
    background-color: #fff;
    border: 1px solid #ccc;
    border-style: solid;
    border-width: 1px;
    border-color: #ccc #a0a0a0 #a0a0a0 #ccc;
/*  border-width: 1px 2px 2px 1px;
    border-style: solid;
    border-color: #aaa #000 #000 #aaa;*/
}

p.thumb a {
    color: #000;
    font-weight: bold;
}

tt {
    font-family: verdana;
    font-size: 10px;
    color: #000;
}

.pagenav {
    position: relative;
    margin: auto;
    clear: both;
    float: left;
    height: 20px;
    width: 100%;
    text-align: left;
}

.pagenav ul, .pagenav li {
    margin: auto;
    padding: 0;
    list-style-type: none;
}

.pagenav li {
    float: left;
    text-align: center;
}

.pagenav li.current a.page {
    font-weight: bold;
    color: #fff;
    background-color: #aaa;
}

.pagenav a.page {
    display: block;
    padding: 2px 5px 2px 5px;
    color: #444;
}

.pagenav a.prev, .pagenav a.next, .pagenav .prev, .pagenav .next {
    display: block;
    width: 65px;
    padding: 2px 5px 2px 5px;
}

.pagenav a:hover {
    color: #000;
}

a {
    color: #999;
    text-decoration: none;
}

a:hover, a:active {
    color: #d0d0d0;
}

a img {
    border-width: 3px;
    border-style: solid;
    border-color: #aaa #ccc #ccc #aaa;
    padding: 0;
    background-color: #ccc;
}

a:hover img {
    border: 3px solid #eee;
}

p.footer {
    position: relative;
    float: right;
    clear: both;
    margin: -20px auto auto auto;
}

</style>
</head>

<body>
<div id="container">
<?

echo '<h1>Photo Gallery: '.$title.'</h1>';


// =============================== GALLERY FUNCTIONS ================================


//-------------------------------- Directory Navigation -----------------------------

/// Gets the list of directories in the current folder
function dir_list($dir)
{
    global $subdir;
    $rootname = str_replace("_", " ", $subdir['0']);
    // name of the root photo gallery
    $dlist = array();
    
    if ($hdir = opendir('./'.$dir)) {
        while ($obj = readdir($hdir)) {
            if(((preg_match("/^\./",$obj)==0) && (!eregi('^(.*).[php]$', $obj))) && is_dir($dir.$obj))
            //if(is_dir($dir.$obj))
                $dlist[] = $obj;
        }
        closedir($hdir);
    } else
        die('Cannot list files for ' . $dir);
    
    asort($dlist);
    echo "<div id='dirnav'><ul>\n";
    
    echo "<li> <a href='?'>$rootname &raquo; </a></li>\n";
    $dirtree = explode("/", $_GET['d']);
    
    // parse directory tree from passed "d"
    if (!$_GET['d'])
        array_shift($dirtree);
    
    // remove redundant "." directory from tree
    foreach($dirtree as $dirsub)
    {
        echo "<li> <a href='?d=$dirparent$dirsub'>$dirsub &raquo; </a></li>\n";
        $dirparent .= $dirsub.'/';
    }
    
    foreach($dlist as $link)
        echo "<li><a href='?d=$dir$link'>$link</a></li>\n";

    echo "</ul>\n</div>\n";
}


//---------------------------------------Page Navigation -----------------------------

/// Writes the number of thumbs, remaining pages, next and prev links
function pagenav()
{
    global $page, $pg, $thumb, $dd, $limit, $numtotal, $numremain;
    
    $page = 0;
    if (!$pg)
        $pg = 1;

    echo "<div class='pagenav'>\n<ul>\n";
    
    if ($thumb > 0)
        echo "<li><a class='prev' href='?".$dd."pg=".($pg-1)."'>&laquo; Prev $limit</a></li>\n";
    else
        echo "<li><div class='prev'>&laquo; Prev $limit</div></li>";
    
    while ($page*$limit < $numtotal) {
        $page ++;
        unset($cur);
        if ($page == $pg)
            $cur = ' class="current"';

        echo "<li$cur><a class='page' href='?".$dd."pg=$page'>".($page)."</a></li>\n";
    }
    
    if ($numremain > $limit)
        echo "<li><a class='next' href='?".$dd."pg=".($pg+1)."'>Next $limit &raquo;</a></li>\n";
    else
        echo "<li><div class='next'>Next $limit &raquo;</div></li>\n";

    echo "</ul>\n</div>";
}


//------------------------------------- Get image file size ----------------------------------

function getfilesize($size) // gets file size for each image
{
    $units = array(' B', ' KB', ' MB', ' GB', ' TB');
    for ($i = 0; $size > 1024; $i++)
        $size /= 1024;

    return round($size, 2).$units[$i];
}

//====================================== END FUNCTIONS =========================================


//------------ Set which folder we are viewing

// d variable is in URL string, used for navigating subdirectories
if($_GET['d'])
{
    $d = $_GET['d'].'/';
    $dd = 'd='.$_GET['d'].'&';
    // for use in passing to URL strings
    $current_dir .= $d;
}

// open the dir to browse for images
opendir($current_dir);

// write out the directory navigation (folder list)
if ($show_folders)
    dir_list($current_dir);



//----------- Create list of Images

$list = array();

if ($dir = opendir('./'.$current_dir)) {
    while ($file = readdir($dir)) {
        //if ((is_file($file)) && ((!eregi('^(.*).[php]$', $file)) && (!eregi('^(\.htaccess)$', $file))))
        //if((eregi('^(.*).[jpg]$', $file)) && (!eregi('^(.*).[php]$', $file)))
        //if (eregi('^(([:alnum:])*)\.((j|J)(p|P)(g|G)))$', $file))
        
        // if file is a JPG
        if(eregi('.jpg', $file))
            $list[] = $file;
    }
    closedir($dir);
}

// sort alpha
asort($list);

// total number of thumbs
$numtotal = count($list);

if ($thumb) {
    $s = 0;
    while ($s < $thumb) {
        array_shift($list);
        $s++;
    }
}

// total remaining thumbs in the array (folder)
$numremain = count($list);

// page links at top of gallery
pagenav();


//--------- Create Thumbnails and Gallery

echo "<div id='thumbnails'>";
foreach($list as $pic)
{
    $fp = $current_dir.$pic;
    if ($i < $thumb+$limit) {
        $image = $currentdir.$pic;
        $i_size = getfilesize(filesize($fp));
        $i_area = getimagesize($fp);
        $i_width = $i_area[0];
        $i_height = $i_area[1];
        //$i_mtime = date("d F Y ", filemtime($fp));
        $name = str_replace('.jpg', '', $pic);
        
        echo "<p class='thumb'><tt>\n";
        echo "  <a href='$current_dir$pic'>";
        echo "  <img src='image_resize.php?i=$d$pic&w=$w&h=$h' alt='$pic' />";
        
        if ($ititle)
            echo "<br /><span class=\"iname\">$name</span>";
        
        if ($idate)
            echo  " - ".date("F d", filemtime($current_dir.$pic));
        
        echo "  </a>\n<br />";
        
        if ($idim)
            echo "<span class=\"idim\">$i_width x $i_height</span>";
        
        if ($idim && $isize)
            echo " - ";
        
        if ($isize)
            echo "<span class=\"isize\">[$i_size]</span>";
        
        echo "</tt></p>\n\n";
        $i++;
    }
}
echo "</div>";

// page links at bottom of gallery
pagenav();

?>

<p class="footer"><a href='http://www.forge22.com'>Photo Gallery</a> by <a href='http://www.forge22.com'>Forge22 Design</a>
</div>

</div>
</body>
</html>
